package com.szc.demo01.controller;
import com.szc.demo01.pojo.Attence;
import com.szc.demo01.pojo.User;
import com.szc.demo01.service.AttenceService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

import com.szc.demo01.controller.base.BaseController;

/**
 * 考勤管理控制器
 * @Author: ShiZhiCheng
 * @Date: 2021/01/13 11:43:25
 */

@Controller
public class AttenceController extends BaseController{
    @Autowired
    private AttenceService attenceService;

    /**
     * 首次访问为系统当前月数据
     * 注意 Date获取的月范围【1-12】
     * AttenceService 中使用的月范围为【0-11】
     * @param model
     * @return
     */
    @GetMapping("/blank_page.html")
    public String show(Model model){
        Date date=new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
        String format = simpleDateFormat.format(date);
        String[] split = format.split("-");
        List<Attence> attenceList = attenceService.ATTENCE_LIST(Integer.parseInt(split[0]), Integer.parseInt(split[1])-1,getUserId());

        model.addAttribute("list",attenceList);
        return "attendance/panels";
    }

    /**
     * 前端传来的月份范围是【1-12】
     * @param month
     * @param year
     * @return
     */
    @GetMapping("/request_for_attenceBymonth")
    @ResponseBody
    public List<Attence> result(@Param("month") Integer month,@Param("e_id")Integer e_id,@Param("year")Integer year){
        List<Attence> attenceList =null;
        if(e_id==null){
            attenceList= attenceService.ATTENCE_LIST(year,month-1,getUserId());
        }else {
            attenceList=attenceService.ATTENCE_LIST(year,month-1,e_id);
        }
        return attenceList;
    }

    /**
     * 下班打卡
     * @return
     */
    @GetMapping(value = "/clock_out")
    @ResponseBody
    public String register(){
        int b = attenceService.clock_out(getUserId());
        switch (b){
            case 0:return "下班打卡成功";
            case 1:return "还未打上班卡，请打上班卡";
            case 2:return "未到下班时间";
            case 3:return "已经打过卡";
            default: return "系统繁忙!";

        }
    }

    @GetMapping(value = "/clock_in")
    @ResponseBody
    public String clock_in(){
        int b = attenceService.clock_in(getUserId());
        switch (b){
            case 0:return "上班打卡成功";
            case 1:return "已经打卡成功";
            default: return "系统繁忙";

        }
    }


}
